package com.news.util;

import java.sql.*;

public class DButil {

    public static String Driver = "com.mysql.jdbc.Driver";
    public static String url = "jdbc:mysql://localhost:3306/news?useUnicode=true&characterEncoding=UTF-8";
    public static String username = "root";
    public static String password = "root";
    public static Statement stmt;
    public static Connection conn;
    public static PreparedStatement pstmt;

    public static void init() {
        try {
            Class.forName(Driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() {
        init();
        Connection conn = null;
        try {
            Class.forName(Driver);
            conn = DriverManager.getConnection(url, username,password);
            System.out.println("----------------successful---------------------");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static Statement getStatement() {
        Statement stmt = null;
        try {
            Class.forName(Driver);
            stmt = getConnection().createStatement();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return stmt;
    }

    public static PreparedStatement getPreparedStatement(String sql) {
        PreparedStatement pstmt = null;
        try {
            Class.forName(Driver);
            pstmt = getConnection().prepareStatement(sql);
            //pstmt.executeUpdate();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pstmt;
    }

    public static void close() {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Connection conn = DButil.getConnection();
        int i = 1;

        //测试数据库连接
        conn = DButil.getConnection();
        try {
            DatabaseMetaData dmd = null;
            dmd = conn.getMetaData();
            System.out.println("数据库名称：" + dmd.getDatabaseProductName());
            System.out.println("数据库版本：" + dmd.getDatabaseProductVersion());
            System.out.println("数据库用户名：" + dmd.getUserName());

            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from project.message");
            ResultSetMetaData rsmd = rs.getMetaData();
            System.out.println("有" + rsmd.getColumnCount() + "字段");
            System.out.println("第2个字段名称是：" + rsmd.getColumnName(2));
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

